using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

public class ReadTXT : MonoBehaviour
{
    private List<String> lines = new List<string>();
    private Mesh mesh;
   
    //txt中读取的数据暂存
    private Vector3[] tempvect;
    private Vector2[] tempuv;
    private Vector3[] tempnormal;
    
    //写入mesh的数据
    private Vector3[] vect;
    private Vector2[] uv;
    private Vector3[] normal;
    private int[] triangles;

    void ReadTxt()
    {
        StreamReader sr = new StreamReader("F:/Project/UnityProject/Shader/Assets/1.txt");
        String text;

        while ((text = sr.ReadLine()) != null)
        {
            lines.Add(text);
        }
        sr.Close();
        //Debug.Log(lines.Count);
    }

    void getVertices()
    {
        List<String> Vertices = new List<string>();
        String text;
        float X;
        float Y;
        float Z;
        //筛选出顶点坐标
        for (int i = 0; i < lines.Count; i++)
        {

            if (lines[i].IndexOf("v ") == 0)
            {
                Vertices.Add(lines[i]);
            }
        }
        
        //新建vector3数组，个数为筛选后的个数
        tempvect=new Vector3[Vertices.Count];
        //字符串拆分，拆分出坐标
        for (int i = 0; i < Vertices.Count; i++)
        {
            text = Vertices[i];
            string[] words = text.Split(' ');
            X = float.Parse(words[1]);
            Y = float.Parse(words[2]);
            Z = float.Parse(words[3]);
            tempvect[i]=new Vector3(X,Y,Z);
        }
        
    }
    
    void getVerticesTexture()
    {
        List<String> VerticesTexture = new List<string>();
        String text;
        float X;
        float Y;
        //筛选出顶点坐标
        for (int i = 0; i < lines.Count; i++)
        {
    
            if (lines[i].IndexOf("vt ") == 0)
            {
                VerticesTexture.Add(lines[i]);
            }
        }
        
        //新建vector2数组，个数为筛选后的个数
        tempuv = new Vector2[VerticesTexture.Count];
        //字符串拆分，拆分出坐标
        for (int i = 0; i < VerticesTexture.Count; i++)
        {
            text = VerticesTexture[i];
            string[] words = text.Split(' ');
            X = float.Parse(words[1]);
            Y = float.Parse(words[2]);
            tempuv[i]=new Vector2(X,Y);
        }
        
    }
    
    void getnormals()
    {
        List<String> normals = new List<string>();
        String text;
        float X;
        float Y;
        float Z;
        //筛选出顶点坐标
        for (int i = 0; i < lines.Count; i++)
        {
    
            if (lines[i].IndexOf("vn ") == 0)
            {
                normals.Add(lines[i]);
            }
        }
        
        //新建vector3数组，个数为筛选后的个数
        tempnormal = new Vector3[normals.Count];
        //字符串拆分，拆分出坐标
        for (int i = 0; i < normals.Count; i++)
        {
            text = normals[i];
            string[] words = text.Split(' ');
            X = float.Parse(words[1]);
            Y = float.Parse(words[2]);
            Z = float.Parse(words[3]);
            tempnormal[i]=new Vector3(X,Y,Z);
        }
        
    }
    
    void getface()
    {
        List<String> faces = new List<string>();
        String text;
        int first;
        int second;
        int third;
        int index = 0; 
        //筛选出顶点坐标
        for (int i = 0; i < lines.Count; i++)
        {

            if (lines[i].IndexOf("f ") == 0)
            {
                faces.Add(lines[i]);
            }
        }
        //新建int数组，个数为筛选后的个数
        triangles = new int[faces.Count * 3];
        vect = new Vector3[faces.Count * 3];
        //字符串拆分，拆分出顺序
        for (int i = 0; i < faces.Count; i++)
        {
            
            String tempA;
            String tempB;
            String tempC;
            string[] A;
            string[] B;
            string[] C;

            text = faces[i];
            string[] words = text.Split(' ');
            
            tempA = words[1];
            tempB = words[2];
            tempC = words[3];

            A = tempA.Split('/');
            B = tempB.Split('/');
            C = tempC.Split('/');
            
            //所有坐标数组下标需-1，因为OBJ指定的坐标数组，是从1开始，而在上一步获取点坐标数组是从0开始
            first = int.Parse(A[0])-1;
            second = int.Parse(B[0])-1;
            third = int.Parse(C[0])-1;

            //按照f行中的顺序排列数组。
            vect[index] = tempvect[first];
            vect[index+1] = tempvect[second];
            vect[index+2] = tempvect[third];
            index += 3;
            
        }

        for (int i = 0; i < triangles.Length-2; i++)
        {
            //因为传入mesh的各个数组已经按照f行处理过顺序，所以直接顺序索引即可
            triangles[i] = i;
            triangles[i + 1] = i + 1;
            triangles[i + 2] = i + 2;
        }
    }
    
    void getUVface()
    {
        List<String> faces = new List<string>();
        String text;
        int first;
        int second;
        int third;
        int index = 0; 
        //筛选出顶点坐标
        for (int i = 0; i < lines.Count; i++)
        {

            if (lines[i].IndexOf("f ") == 0)
            {
                faces.Add(lines[i]);
            }
        }
        //新建vector2数组，个数为筛选后的个数
        uv = new Vector2[faces.Count*3];
        //字符串拆分，拆分出顺序
        for (int i = 0; i < faces.Count; i++)
        {
            String tempA;
            String tempB;
            String tempC;
            string[] A;
            string[] B;
            string[] C;

            text = faces[i];
            string[] words = text.Split(' ');
            
            tempA = words[1];
            tempB = words[2];
            tempC = words[3];

            A = tempA.Split('/');
            B = tempB.Split('/');
            C = tempC.Split('/');
            
            //所有坐标数组下标需-1，因为OBJ指定的坐标数组，是从1开始，而在上一步获取点坐标数组是从0开始
            first = int.Parse(A[1])-1;
            second = int.Parse(B[1])-1;
            third = int.Parse(C[1])-1;
            
            //按照f行中的顺序排列数组。
            uv[index] = tempuv[first];
            uv[index+1] = tempuv[second];
            uv[index+2] = tempuv[third];
            index += 3;
        }
    }
    
    void getnormalface()
    {
        List<String> faces = new List<string>();
        String text;
        int first;
        int second;
        int third;
        int index = 0; 
        //筛选出顶点坐标
        for (int i = 0; i < lines.Count; i++)
        {

            if (lines[i].IndexOf("f ") == 0)
            {
                faces.Add(lines[i]);
            }
        }
        //新建vector3数组，个数为筛选后的个数
        normal = new Vector3[faces.Count*3];
        //字符串拆分，拆分出顺序
        for (int i = 0; i < faces.Count; i++)
        {
            String tempA;
            String tempB;
            String tempC;
            string[] A;
            string[] B;
            string[] C;

            text = faces[i];
            string[] words = text.Split(' ');
            
            tempA = words[1];
            tempB = words[2];
            tempC = words[3];

            A = tempA.Split('/');
            B = tempB.Split('/');
            C = tempC.Split('/');
            
            //所有坐标数组下标需-1，因为OBJ指定的坐标数组，是从1开始，而在上一步获取点坐标数组是从0开始
            first = int.Parse(A[2])-1;
            second = int.Parse(B[2])-1;
            third = int.Parse(C[2])-1;
            
            //按照f行中的顺序排列数组。
            normal[index] = tempnormal[first];
            normal[index+1] = tempnormal[second];
            normal[index+2] = tempnormal[third];
            
            index += 3;
        }
    }
    
    
    
    
    void Start()
    {
        GetComponent<MeshFilter>().mesh=mesh=new Mesh();
        //读取txt存入数组
        ReadTxt();
        getVertices();
        getVerticesTexture();
        getnormals();
        
        //按照f行将要传入mesh的数据提前进行排列
        getface();
        getUVface();
        getnormalface();
        
        mesh.vertices = vect;
        mesh.uv = uv;
        mesh.normals = normal;
        //triangles直接顺序传入，因为vertices uv normals都已经处理好顺序，所以直接顺序索引triangles即可
        mesh.triangles = triangles;
    }

}
